﻿using UnityEngine;
using System.Collections;

public class missionController : MonoBehaviour {

	public AudioSource src;
	public GameObject player;
	public GameObject target;
	public AudioClip missionIncoming;
	public AudioClip missionOngoing;
	public AudioClip missionCompleted;
	public AudioClip missionFailed;
	private int state;
	private float timer;

	// Use this for initialization
	void Start () {
		state = 0;
		timer = 0;
		src.Stop ();
	}
	
	// Update is called once per frame
	void Update () {
		if (state == 0 && Input.GetKeyDown("m"))
		{
			state = 1;
			src.audio.clip = missionIncoming;
			src.loop = true;
			src.Play();
		}
		else if (state == 1 && Input.GetKeyDown("m"))
		{
			state = 2;
			src.audio.clip = missionOngoing;
			src.loop = true;
			src.Play();
			timer = 3000;
			src.audio.pitch = 1;
		}
		else if (state == 2 && timer <= 0)
		{
			state = 0;
			src.audio.clip = missionFailed;
			src.loop = false;
			src.audio.pitch = 1;
			src.Play();
		}
		else if (state == 2 && Vector3.Distance(player.transform.position, target.transform.position) < 3)
		{
			state = 0;
			src.audio.clip = missionCompleted;
			src.loop = false;
			src.audio.pitch = 1;
			src.Play();
			timer = 0;
		}
	}

	void FixedUpdate()
	{
		if (timer > 0)
		{
			timer = timer-1;
			src.audio.pitch = src.audio.pitch * 1.0003f;
		}
	}
}
